package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    // /employee/list?currentPage=1&pageSize=5&keyword=a&deptId=1
    // 处理分页查询员工的请求
    @RequestMapping("/list")
    @RequiredPermission(name = "员工列表",expression = "employee.list")
    public String list(Model model, @ModelAttribute("qo")EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        // 查询所有部门数据，存到 Model 中
        model.addAttribute("departments", departmentService.listAll());
        return "employee/list"; // /WEB-INF/views/employee/list.jsp
    }

    // 处理删除员工的请求 /employee/delete?id=1
    @RequiredPermission(name = "员工删除",expression = "employee.delete")


   /* @RequestMapping("/employee/delete")
    public JsonResult delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return new JsonResult(true, "成功");
    }*/


    @RequestMapping(value="/employees",
            method = RequestMethod.DELETE)
    public JsonResult delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return new JsonResult(true, "成功");
    }


    // 处理去新增或去修改的请求
    @RequiredPermission(name = "员工编辑页面",expression = "employee.input")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        // 查询所有部门数据，存到 Model 中
        model.addAttribute("departments", departmentService.listAll());
        model.addAttribute("allRoles",roleService.selectAll());
        if (id != null) { // 去修改
            model.addAttribute("employee",
                employeeService.get(id));
            model.addAttribute("selfRoles",roleService.selectByEmpId(id));
        }
        return "employee/input";
    }

    // 处理保存或修改请求
    @RequiredPermission(name = "员工新增/更新",expression = "employee.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids) {
        if (employee.getId() == null) { // 保存
            employeeService.save(employee,ids);
        } else { // 修改
            employeeService.update(employee,ids);
        }
        return "redirect:/employee/list"; // 重定向，浏览器再发一次请求
    }
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        String fileName = new String("员工通讯录.xls".getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        employeeService.exportXls(response.getOutputStream());
    }
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
